package com.slz3.business.frame.conf;

import com.google.gson.Gson;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.redis.Jedis;
import com.slz3.business.frame.redis.RK;
import com.slz3.business.module.configManagement.conSiteCity.domain.ConSiteCity;
import com.slz3.business.module.configManagement.conSiteCounty.domain.ConSiteCounty;
import com.slz3.business.module.configManagement.conSiteProvince.domain.ConSiteProvince;
import org.springframework.boot.CommandLineRunner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 初始化pg省市县数据到redis
 */
//@Component
public class AddressConfig implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        List<ConSiteProvince> conSiteProvinceList = Repositorys.conSiteProvince.findAll();
        Map map = new HashMap<String,String>();
        for (ConSiteProvince conSiteProvince: conSiteProvinceList) {
            UUID uuid = conSiteProvince.getUuid();
            map.put(uuid.toString(),new Gson().toJson(conSiteProvince));
        }
        //hash结构数据
        Jedis.db0.set(RK.CON_SITE_PROVINCE,map);
        map.clear();
        List<ConSiteCity> conSiteCityList = Repositorys.conSiteCity.findAll();
        for (ConSiteCity conSiteCity: conSiteCityList) {
            UUID uuid = conSiteCity.getUuid();
            UUID puuid = conSiteCity.getSerProvinceId();
            String key = puuid.toString().concat(":").concat(uuid.toString());
            map.put(key,new Gson().toJson(conSiteCity));
        }
        //hash结构数据
        Jedis.db0.set(RK.CON_SITE_CITY,map);
        map.clear();
        List<ConSiteCounty> conSiteCountyList = Repositorys.conSiteCounty.findAll();
        for (ConSiteCounty conSiteCounty: conSiteCountyList) {
            UUID uuid = conSiteCounty.getUuid();
            UUID cuuid = conSiteCounty.getSerCityId();
            String key = cuuid.toString().concat(":").concat(uuid.toString());
            map.put(key,new Gson().toJson(conSiteCounty));
        }
        Jedis.db0.set(RK.CON_SITE_COUNTRY,map);
    }
}
